package bpm.dao.impl.manage;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import bpm.dao.manage.DictionaryTypeDao;
import bpm.domain.common.PageObject;
import bpm.domain.manage.DictionaryType;
@Repository("dictionaryTypeDao")
public class DictionaryTypeDaoImpl implements DictionaryTypeDao {
	@Resource private JdbcTemplate jdbcTemplate;
	private static final Logger logger=Logger.getLogger(DictionaryTypeDaoImpl.class);
	public void delete(String[] dictTypeIds) {
		StringBuffer sql=new StringBuffer();
		sql.append(" delete from BPM_AD_DICTIONARY_TYPE where id in ( ");
		for (int i = 0; i < dictTypeIds.length; i++) {
			if(i!= dictTypeIds.length-1){
				sql.append(" ?, ");
			}else{
				sql.append("?  )");
			}
		}
		logger.debug("DeleteSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),dictTypeIds);
	}
	@SuppressWarnings("unchecked")
	public List<DictionaryType> findAll(PageObject pageObject) {
		if(pageObject!=null){
			String count="select count(*) from BPM_AD_DICTIONARY_TYPE";
			StringBuffer sql=new StringBuffer();
			sql.append(" select ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION from ( ")
			.append(" select rownumber() over(order by SORT_INDEX asc) as num,ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION from ")
			.append(" BPM_AD_DICTIONARY_TYPE ) where num>? and num<= ?");
			logger.debug("QuerySQL:"+sql.toString());
			PageObject.setPageObject(jdbcTemplate, count, pageObject);
			return  this.jdbcTemplate.query(sql.toString(), new Object[]{pageObject.getStart(),pageObject.getEnd()},
					new DictionaryTypeRowMapper());
		}else{
			StringBuffer sql=new StringBuffer();
			sql.append(" select ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION from ")
			.append(" BPM_AD_DICTIONARY_TYPE  order by SORT_INDEX asc ");
			logger.debug("QuerySQL:"+sql.toString());
			return  this.jdbcTemplate.query(sql.toString(),
					new DictionaryTypeRowMapper());
		}
	}

	public DictionaryType findDictById(String dictTypeId) {
		String sql="select ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION from BPM_AD_DICTIONARY_TYPE "
			+" where ID= ? ";
		logger.debug("QuerySQL:"+sql);
		List<?> list=this.jdbcTemplate.query(sql, new Object[]{dictTypeId},new DictionaryTypeRowMapper());
		if(list!=null&&list.size()>0){
			return (DictionaryType)list.get(0);
		}else{
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<DictionaryType> findDictTypeByIdOrName(DictionaryType dictType,
			PageObject pageObject) {
		StringBuffer countSQL=new StringBuffer();
		countSQL.append("select count(*) from BPM_AD_DICTIONARY_TYPE ");
		StringBuffer sql=new StringBuffer();
		sql.append(" select ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION  from ( ")
			.append("select rownumber() over(order by SORT_INDEX asc) as num,ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION ")
			.append(" from BPM_AD_DICTIONARY_TYPE ");
		String id = dictType.getId();
		String name= dictType.getName();
		if(id!=null&&!"".equals(id)||name!=null&&!"".equals(name)){
			countSQL.append("  where ");
			sql.append(" where ");
			if(id !=null &&!"".equals(id)){
				countSQL.append(" ID like '%").append(id).append("%' and ");
				sql.append(" ID like  '%").append(id).append("%' and ");
			}
			if(name!=null&&!"".equals(name)){
				countSQL.append(" NAME like '%").append(name).append("%' and ");
				sql.append(" NAME like '%").append(name).append("%' and ");
			}
			String count=countSQL.substring(0, countSQL.lastIndexOf("and"));
			countSQL=new StringBuffer();
			countSQL.append(count);
			String sqlTemp= sql.substring(0, sql.lastIndexOf("and"));
			sql=new StringBuffer();
			sql.append(sqlTemp);
		}	
		
		sql.append(" ) where num > ? and num <= ?");
		logger.debug("CountSQL:"+countSQL.toString());
		logger.debug("QuerySQL:"+sql.toString());
		PageObject.setPageObject(jdbcTemplate, countSQL.toString(), pageObject);
		
		return this.jdbcTemplate.query(sql.toString(),new Object[]{pageObject.getStart(),pageObject.getEnd()},
				new DictionaryTypeRowMapper());
	}

	public void insert(DictionaryType dictType) {
		String sql="insert into BPM_AD_DICTIONARY_TYPE(ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION)"
		+" values (?,?,?,?,?,?)";
		logger.debug("InsertSQL:"+sql);
		this.jdbcTemplate.update(sql, new Object[]{
				dictType.getId(),dictType.getName(),dictType.getSortIndex(),
				new java.sql.Timestamp(dictType.getCreateTime().getTime()),
				new java.sql.Timestamp(dictType.getLastUpdateTime().getTime()),
				dictType.getDescription()
		});
	}

	public void update(DictionaryType dictType) {
		StringBuffer sql=new StringBuffer();
		sql.append(" update BPM_AD_DICTIONARY_TYPE set NAME=?,SORT_INDEX=?")
		.append(" ,LASTUPDATE_TIME=?,DESCRIPTION=? where ID=?");
		logger.debug("UpdateSQL:"+sql.toString());
		
		this.jdbcTemplate.update(sql.toString(),new Object[]{
			dictType.getName(),dictType.getSortIndex(),
			new java.sql.Timestamp(dictType.getLastUpdateTime().getTime()),
			dictType.getDescription(),dictType.getId()
		});
	}
	@SuppressWarnings("unchecked")
	public List<DictionaryType> findDictionaryTypes(String[] dictTypeIds) {
		StringBuffer sql=new StringBuffer();
		sql.append("select ID, NAME, SORT_INDEX, CREATE_TIME, LASTUPDATE_TIME, DESCRIPTION from BPM_AD_DICTIONARY_TYPE ")
			.append(" where ID in ( ");
		for (int i = 0; i < dictTypeIds.length; i++) {
			if(i!=dictTypeIds.length-1){
				sql.append("?,");
			}else{
				sql.append("? )");
			}
		}
		logger.debug("QuerySQL:"+sql);
		return this.jdbcTemplate.query(sql.toString(), dictTypeIds,new DictionaryTypeRowMapper());
	}

}
